<?php
/**
 * Created by lihuan.
 * User: lihuan
 * Date: 2018/3/29 0029
 * Time: 下午 2:43
 */
namespace app\wxmsg\controller;

class TextMsg {

    //处理动作事件（比如关注、取消关注）
    public function handelMsg($postObj){
    
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $msgType = $postObj->MsgType;
        $keyword = trim($postObj->Content);
        $time = time();
    
        if (! empty($keyword)) {
            $resultStr = sprintf($this->getTextTemplate(), $fromUsername, $toUsername, $time, $msgType, $this->newContent());
            echo $resultStr;
        } else {
            echo "请输入内容";
        }
    }
    
    private function getTextTemplate(){
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>0</FuncFlag>
                    </xml>";
        return $textTpl;
    }
    
    private function newContent(){
        $content = "欢迎来到此公众号!";
        return $content;
    }
}